<?php
class RegistrationHandler extends CI_Model{
	public function doRegistration($user){
		$this->load->model('userDAL');	
		$salt = $this->generateSalt();
		$hashPassword = $this->hashPassword($salt, $user->getPassword());
		// Check if username exists
		// TODO: send in userobjekt and rewrite userExists() method
		if($this->userDAL->usernameExists($user) == FALSE){
			// Return true if registration was successfull
			if($this->userDAL->addUser($user->getFirstname(), $user->getLastname(), $user->getEmail(), $user->getUsername(), $hashPassword) == TRUE){
			
				return TRUE;
			}
			// TODO: Errormessage - registration failed
			else return FALSE;
		}
		// TODO: Errormessage - username exists
		else return FALSE;
	}
	public function deleteUser($username){
		$this->load->model('userDAL');
		if($this->userDAL->deleteUser($username)){
			return TRUE;
		}
		// TODO: Erroressage - deleting user failed
		else return FALSE;
	}
	public function generateSalt(){
		$salt = hash('sha1', uniqid(mt_rand(), true));
		return $salt;
	}
	public function hashPassword($salt, $password){
		$hash = $salt . $password;
		$hash = hash('sha1', $hash);
		return $salt . $hash;
	}
}